﻿Imports Microsoft.Reporting.WinForms
Imports System.IO

Public Class frmResultado
    Public modeForm As Integer
    Public frmParent As frmMResultado
    Public detalle As New List(Of frmJson.objRoot)

    Public Shared Function getDetalle() As List(Of frmJson.objRoot)
        Dim lista As New List(Of frmJson.objRoot)()
        Dim evaluacion As New clssEvaluaciones
        Dim dstRecord As New DataSet
        Dim strId As String = frmResultado.frmParent.dgvResultado.Rows(frmResultado.frmParent.dgvResultado.CurrentRow.Index).Cells(0).Value.ToString
        Dim Rootobject() As clssEvaluaciones.sessionR

        dstRecord.Tables.Add("resultado")
        mdlCRUD.loadRecord(dstRecord, "resultado", "resultado", "resultado_id = '" & strId & "'", Nothing)

        Dim arrRow As Array
        arrRow = dstRecord.Tables("resultado").Rows(0).ItemArray.ToArray

        Rootobject = evaluacion.getEvaluacion(arrRow(3).ToString)
        For i = Rootobject.Count - 1 To 0 Step -1
            'ultimo += detalleEvaluacion(Rootobject(i).id.ToString, Rootobject(i).ojb, forma, ultimo)
            Dim pregrunta() As Pregunta = Rootobject(i).ojb
            For j = 0 To pregrunta.Count - 1
                pregrunta(j).id.ToString()
                Dim dstRecord2 As New DataSet
                dstRecord2.Tables.Add("pregunta")
                mdlCRUD.loadRecord(dstRecord2, "pregunta", "pregunta", "pregunta_id = '" & pregrunta(j).id & "'", "")
                If (dstRecord2.Tables("pregunta").Rows.Count > 0) Then
                    arrRow = dstRecord2.Tables("pregunta").Rows(0).ItemArray.ToArray
                    Dim item As New frmJson.objRoot
                    If (j = 0) Then
                        item.idOLP = Rootobject(i).id.ToString
                    End If
                    item.id = arrRow(1)
                    item.respuesta = pregrunta(j).respuesta
                    item.respuestaOLP = pregrunta(j).respuestaOLP
                    lista.Add(item)
                End If
            Next j
        Next i
        Return lista
    End Function
    Private Sub frmResultado_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Select Case Me.modeForm
            Case 1 'Consultar
                frmParent.tsbConsultar.Enabled = True
            Case 2 'Editar
                frmParent.tsbEditar.Enabled = True
        End Select
    End Sub

    'Private Sub frmResultado_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    '    'Me.btnCancelar.Text = "Cancelar"
    '    'Me.btnGuardar.Visible = True

    '    Select Case Me.modeForm

    '        Case 1 'Consultar
    '            'Me.txtCodigo.Text = Me.frmParent.arrConsulta(0)

    '            'If Me.frmParent.arrConsulta(1) <> "" Then
    '            '    Me.cmbTipoPrueba.SelectedValue = Me.frmParent.arrConsulta(1)
    '            'End If

    '            'Me.btnCancelar.Text = "Continuar"
    '            'Me.btnGuardar.Visible = False

    '        Case 2 'Editar
    '            Dim evaluacion As New configuracionEvaluaciones
    '            Dim dstRecord As New DataSet
    '            Dim strId As String = Me.frmParent.dgvResultado.Rows(Me.frmParent.dgvResultado.CurrentRow.Index).Cells(0).Value.ToString

    '            dstRecord.Tables.Add("resultado")
    '            mdlCRUD.loadRecord(dstRecord, "resultado", "resultado", "resultado_id = '" & strId & "'", Nothing)

    '            Dim arrRow As Array
    '            arrRow = dstRecord.Tables("resultado").Rows(0).ItemArray.ToArray

    '            evaluacion.getEvaluacion(arrRow(3).ToString, Me)
    '            'Me.lblEdCodigo.Text = arrRow(0).ToString
    '            'Me.txtCodigo.Text = arrRow(0).ToString

    '            'If arrRow(1).ToString <> "" Then
    '            '    Me.cmbTipoPrueba.SelectedValue = arrRow(1).ToString
    '            'End If

    '            'Me.txtNombre.Text = arrRow(2).ToString
    '            'Me.txtDescripcion.Text = arrRow(3).ToString


    '    End Select
    '    Me.ReportViewer1.RefreshReport()
    'End Sub
    Private Sub frmResultado_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        'Me.btnCancelar.Text = "Cancelar"
        'Me.btnGuardar.Visible = True

        Select Case Me.modeForm

            Case 1 'Consultar
                'Me.txtCodigo.Text = Me.frmParent.arrConsulta(0)

                'If Me.frmParent.arrConsulta(1) <> "" Then
                '    Me.cmbTipoPrueba.SelectedValue = Me.frmParent.arrConsulta(1)
                'End If

                'Me.btnCancelar.Text = "Continuar"
                'Me.btnGuardar.Visible = False

            Case 2 'Editar
                'Limpiemos el DataSource del informe
                ReportViewer1.LocalReport.DataSources.Clear()
                ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("DataSet1", getDetalle()))
                Me.ReportViewer1.RefreshReport()
        End Select

    End Sub
    'Private Sub btnCancelar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancelar.Click
    '    Select Case Me.modeForm
    '        Case 1 'Nuevo

    '        Case 2 'Consultar
    '            'Me.frmParent.arrConsulta(0) = Trim(Me.txtCodigo.Text)

    '            'If Me.cmbTipoPrueba.SelectedIndex <> -1 Then
    '            '    Me.frmParent.arrConsulta(1) = Trim(Me.cmbTipoPrueba.SelectedValue.ToString)
    '            'End If

    '            'Me.frmParent.arrConsulta(2) = Trim(Me.txtNombre.Text)
    '            'Me.frmParent.arrConsulta(3) = Trim(Me.txtDescripcion.Text)

    '        Case 3 'Editar

    '    End Select


    '    Me.Close()
    'End Sub
    Sub save_pdf(ByVal Id As String)
        Dim lista As New List(Of frmJson.objRoot)()
        Dim evaluacion As New clssEvaluaciones
        Dim dstRecord As New DataSet
        Dim strId As String = Id
        Dim Rootobject() As clssEvaluaciones.sessionR

        dstRecord.Tables.Add("resultado")
        mdlCRUD.loadRecord(dstRecord, "resultado", "resultado", "resultado_id = '" & strId & "'", Nothing)

        Dim arrRow As Array
        arrRow = dstRecord.Tables("resultado").Rows(0).ItemArray.ToArray

        Rootobject = evaluacion.getEvaluacion(arrRow(3).ToString)
        For i = Rootobject.Count - 1 To 0 Step -1
            'ultimo += detalleEvaluacion(Rootobject(i).id.ToString, Rootobject(i).ojb, forma, ultimo)
            Dim pregrunta() As Pregunta = Rootobject(i).ojb
            For j = 0 To pregrunta.Count - 1
                pregrunta(j).id.ToString()
                Dim dstRecord2 As New DataSet
                dstRecord2.Tables.Add("pregunta")
                mdlCRUD.loadRecord(dstRecord2, "pregunta", "pregunta", "pregunta_id = '" & pregrunta(j).id & "'", "")
                If (dstRecord2.Tables("pregunta").Rows.Count > 0) Then
                    arrRow = dstRecord2.Tables("pregunta").Rows(0).ItemArray.ToArray
                    Dim item As New frmJson.objRoot
                    If (j = 0) Then
                        item.idOLP = Rootobject(i).id.ToString
                    End If
                    item.id = arrRow(1)
                    item.respuesta = pregrunta(j).respuesta
                    item.respuestaOLP = pregrunta(j).respuestaOLP
                    lista.Add(item)
                End If
            Next j
        Next i
        ReportViewer1.LocalReport.DataSources.Clear()
        ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("DataSet1", lista))
        Dim byteViewer As Byte() = ReportViewer1.LocalReport.Render("PDF")
        Dim saveFileDialog1 As New SaveFileDialog()
        saveFileDialog1.Filter = "*PDF files (*.pdf)|*.pdf"
        saveFileDialog1.FilterIndex = 2
        saveFileDialog1.RestoreDirectory = True
        Dim newFile As New FileStream("C:\Users\jortiz\Documents\" & mdlEvaluaciones.idcertificado & "_" & mdlEvaluaciones.idprueba & "_" & clssIdentidad.sID & ".pdf", FileMode.Create)
        newFile.Write(byteViewer, 0, byteViewer.Length)
        newFile.Close()
        Me.ReportViewer1.RefreshReport()
    End Sub
End Class
